 var  $unStnLineRel = $('#unStnLineRel'),
      $selStnLineRel = $('#selStnLineRel'),
      $selStnLineRelDown = $('#selStnLineRelDown'),
      corpId;

require(['grid','select2','date'], function () {
	$('form').initForm();
    //grid初始化
    function gridInit() {
    	$unStnLineRel.grid({
	        url: "/stnLineRel/getUnSelStnLineRel.htm",
	        colModel: [
	            {name: "stnId", index: "stnId", label: 'id',hidden: true},
	            {name: "stnCd", index: "stnCd", label: '站点编号',width:100},
	            {name: "stnName", index: "stnName", label: '站点名称',width:100,formatter:function (cellvalue, options, rowObject){
	            	rowObject.stnName = rowObject.stnName.replace(/(︹)(\S+)(︺)/,'($2)');
					return rowObject.stnName;
				}},
                {name: "direction",label: '方向', width: 80,dict: "LIT00000"},
	        ],
	        showPager:false,
			width:395,
			height:376
	    });
    	
    	//上行
    	$selStnLineRel.grid({
	        url: "/stnLineRel/getSelStnLineRel.htm",
	        colModel: [
	            {name: "stnId", index: "stnId", label: 'id',hidden: true},
	            {name: "stnCd", index: "stnCd", label: '站点编号',width:100},
	            {name: "stnName", index: "stnName", label: '站点名称',width:100, formatter:function (cellvalue, options, rowObject){
	            	rowObject.stnName = rowObject.stnName.replace(/(︹)(\S+)(︺)/,'($2)');
//					rowObject.stnName= rowObject.stnName.replace("︹","(");
//					rowObject.stnName= rowObject.stnName.replace("︺",")");
					return rowObject.stnName;
				}},
                {name: "direction",label: '方向', width: 80,dict: "LIT00000"},
	            {name: "stnSn", index: "stnSn", label: '序号',width:50}
	        ],
	    	showPager: false,
			width:393,
			height:144
	    });
    	
    	//下行
    	$selStnLineRelDown.grid({
	        url: "/stnLineRel/getSelStnLineRelDown.htm",
	        colModel: [
	            {name: "stnId", index: "stnId", label: 'id',hidden: true},
	            {name: "stnCd", index: "stnCd", label: '站点编号',width:100},
	            {name: "stnName", index: "stnName", label: '站点名称',width:100, formatter:function (cellvalue, options, rowObject){
	            	rowObject.stnName = rowObject.stnName.replace(/(︹)(\S+)(︺)/,'($2)');
					return rowObject.stnName;
				}},
                {name: "direction",label: '方向', width: 80,dict: "LIT00000"},
	            {name: "stnSn", index: "stnSn", label: '序号',width:50}
	        ],
	    	showPager: false,
			width:393,
			height:144
	    });
    }

    addInit = function (strtusDateHtml,selCorpId){
    	corpId=selCorpId;
		$("#remark").val("");
		$("#stnType option[value='LIT00003']").removeAttr("selected").trigger('change');
		$("#startDate").val("");
		var nodes=commonFun.getLeftTreeSelectedNodes();
		if(nodes.length!=1){
			$.messager.alert("提示","只能选择一条线路");
		}else{
			$("#strtusDate").html(strtusDateHtml);
			//重新载入
			$unStnLineRel.grid("clearGridData");
			$unStnLineRel.grid('setGridParam', {datatype:"json", postData:{"corpId":corpId}}).trigger("reloadGrid");
			jQuery("#selStnLineRel").grid("clearGridData");
			jQuery("#selStnLineRelDown").grid("clearGridData");
		}
    };
   
    add = function(fn) {
    	var nodes=commonFun.getLeftTreeSelectedNodes();
		var selLineId,selLineName;
		if(nodes[0].type==='LINE'){
			selLineId=nodes[0].id;
			selLineName=nodes[0].name;
		}
		//获取所有grid所有数据上行
		var rowData= $selStnLineRel.getRowData(), stnId ="",stnName ="",direction ="",stnSn ="";
		for (var i = 0; i < rowData.length; i ++) {
			stnId +=rowData[i].stnId+",";
			stnName +=rowData[i].stnName+",";
            direction +=rowData[i].direction+",";
			stnSn +=rowData[i].stnSn+",";
		};
		//获取所有grid所有数据下行
		var rowDataDown= $selStnLineRelDown.getRowData();
		var stnIdDown ="", stnNameDown ="",loDetailInfoDown ="",stnSnDown ="";
		for (var i = 0; i < rowDataDown.length; i ++) {
			stnIdDown +=rowDataDown[i].stnId+",";
			stnNameDown +=rowDataDown[i].stnName+",";
			loDetailInfoDown +=rowDataDown[i].direction+",";
			stnSnDown +=rowDataDown[i].stnSn+",";
		}
		if($("#stnType").val()=="LIT00003"){
			rowDataDown=null;
		}
		if($("#startDate").val() != "" && rowData!="" && rowDataDown!="") {
			$.ajaxExt({
				url:"/stnLineRel/checkVersion.htm",
				data:{"strtusDate":$("#startDate").val(),"lineId":selLineId}
			}).done(function(result){
				if(result>0){
					$.messager.alert("提示","线路:"+selLineName+" 启用日期为:"+$("#startDate").val()+"的数据已经存在");
					return;
				}else{
					$.ajaxExt({
						url:"/stnLineRel/saveStnLineRel.htm",
						data:{"strtusDate":$("#startDate").val(),"lineId":selLineId,"remark":$("#remark").val(),"stnId":stnId,"stnName":stnName,
							  "direction":direction,"stnSn":stnSn,"stnIdDown":stnIdDown,"stnNameDown":stnNameDown,
							  "loDetailInfoDown":loDetailInfoDown,"stnSnDown":stnSnDown,"stnType":$("#stnType").val()}
					}).done(function(){
							if(fn){
								fn();
							}
						});
					} 
			});
		}else{
			$.messager.alert("提示","启用日期、站点必填");
		}
   	};
   
    editInit = function (initStrtusDate,strtusDateHtml,selCorpId){
    	corpId=selCorpId;
    	$("#startDate").val("");
	  	var strtusdate = initStrtusDate;
	  	strtusdate=strtusdate.replace(/-/g, "/");  
	  	var d = new Date();
	  	var currentDate="";
	  	var yyyy = d.getFullYear(); 
	  	var mm = d.getMonth()+1; 
	  	var dd = d.getDate();
	  	mm = mm>9?mm.toString():'0' + mm;
	  	dd = dd>9?dd.toString():'0' + dd;
	  	currentDate=yyyy + "/" + mm + "/" + dd;
	  	if(strtusdate<currentDate){
	  		 $.messager.alert("提示","历史版本不能修改,请检查!");
	  		 return ;
	  	}else{
	  		$("#strtusDate").html(strtusDateHtml);
	  		var nodes=commonFun.getLeftTreeSelectedNodes();
	  		var selLineId,selLineName,selCorpId;
	  		if(nodes[0].type==='LINE'){
	  			selLineId=nodes[0].id;
	  			selLineName=nodes[0].name;
	  			selCorpId=nodes[0].corpId;
	  		}
	  		//清空
	  		jQuery("#selStnLineRel").grid("clearGridData");
	  		//重新载入
	  		$unStnLineRel.grid("clearGridData");
	  		$unStnLineRel.grid('setGridParam', {datatype:"json", postData:{"corpId":corpId,"lineId":selLineId,"strtusDate":initStrtusDate}}).trigger("reloadGrid");
	  		$selStnLineRel.grid('setGridParam', {datatype:"json", postData:{"lineId":selLineId,"strtusDate":initStrtusDate}}).trigger("reloadGrid");
	  		$selStnLineRelDown.grid('setGridParam', {datatype:"json", postData:{"lineId":selLineId,"strtusDate":initStrtusDate},
	  			loadComplete:function(data){
	  				$("#stnType option[value='"+(data ? 'LIT00001' : 'LIT00003') + "']").attr("selected", "selected").trigger('change');
	  			}
	  		}).trigger("reloadGrid");
	  		$.ajaxExt({
	  			url : "/stnLineRel/upUpdate.htm",
	  			data : {"lineId":selLineId,"strtusDate":initStrtusDate}
	  		}).done(function(result){
	  			var startDate="",remark="";
	  			$(result).each(function(i,item) { 
	  				startDate=item.strtusDate;
	  				remark=item.remark;
	  			})
	  			$("#lineId").text(selLineName);
	  			$("#startDate").val(startDate);
	  			$("#remark").val(remark);
	  		});
	  	}
    }
    
    edit = function(fn) {
    	var nodes=commonFun.getLeftTreeSelectedNodes();
		var selLineId,selLineName;
		if(nodes[0].type==='LINE'){
			selLineId=nodes[0].id;
			selLineName=nodes[0].name;
		}
		//获取所有grid所有数据上行
		var rowData= $selStnLineRel.getRowData();
		var stnId ="",stnName ="",direction ="",stnSn ="";
		for (var i = 0; i < rowData.length; i ++) {
			stnId +=rowData[i].stnId+",";
			stnName +=rowData[i].stnName+",";
            direction +=rowData[i].direction+",";
			stnSn +=rowData[i].stnSn+",";
		}
		//获取所有grid所有数据下行
		var rowDataDown= $selStnLineRelDown.getRowData();
		var stnIdDown ="",stnNameDown ="",loDetailInfoDown ="",stnSnDown ="";
		for (var i = 0; i < rowDataDown.length; i ++) {
			stnIdDown +=rowDataDown[i].stnId+",";
			stnNameDown +=rowDataDown[i].stnName+",";
			loDetailInfoDown +=rowDataDown[i].direction+",";
			stnSnDown +=rowDataDown[i].stnSn+",";
		}
		if($("#stnType").val()=="LIT00003"){
			rowDataDown=null;
		}
		if($("#startDate").val() != "" && rowData!="" && rowDataDown!="") {
			$.ajaxExt({
				url:"/stnLineRel/checkVersion.htm",
				data:{"strtusDate":$("#startDate").val(),"lineId":selLineId}
			}).done(function(result){
				if(result>0){
					$.messager.confirm("提示","线路:"+selLineName+" 启用日期为:"+$("#startDate").val()+"的数据已经存在是否覆盖",function(r){
						if(r){
							//保存动作
							$.ajaxExt({
								url:"/stnLineRel/saveStnLineRel.htm",
								data:{"strtusDate":$("#startDate").val(),"lineId":selLineId,"remark":$("#remark").val(),"stnId":stnId,"stnName":stnName,
									  "direction":direction,"stnSn":stnSn,"stnIdDown":stnIdDown,"stnNameDown":stnNameDown,
									  "loDetailInfoDown":loDetailInfoDown,"stnSnDown":stnSnDown,"stnType":$("#stnType").val()}
							}).done(function(){ 
									if(fn){
										fn();
									}
								});
							}
						});
					}
				});
		}else{
			$.messager.alert("提示","启用日期、站点必填");
		}
   	};
    
	//上行环行切换
	$("#stnType").change(function(){
		var type=$(this).children("option:selected").val();
		if(type=="LIT00003"){
			$("#hiddenUp").hide();
			$("#hiddenUp2").hide();
		}
		if(type=="LIT00001"){
			$("#hiddenUp").show();
			$("#hiddenUp2").show();
		}
	});
	
	//弹出对话框的站点名搜索
	$("#searchStnNameBtn").click(function(){
		showStn($("#stnCdOrName").val(),corpId);
	});

	//根据站点名搜索对应站点
	function showStn(stnCdOrName,corpId){
		//先清空jqgrid
		$unStnLineRel.jqGrid("clearGridData");
		$unStnLineRel.jqGrid('setGridParam', {datatype:"json",postData:{"corpId":corpId, "stnCdOrName":stnCdOrName}}).trigger("reloadGrid");
	}
	//根据版本号导入数据
	$("#importStrtusDate").click(function(){
		var nodes=commonFun.getLeftTreeSelectedNodes();
		var selLineId;
		if(nodes[0].type==='LINE'){
			selLineId=nodes[0].id;
		}
		//showStrtusDateStn(selLineId,$("#strtusDate").val());
		$selStnLineRel.jqGrid('setGridParam', {datatype:"json",postData:{lineId:selLineId,strtusDate:$("#strtusDate").val()}}).trigger("reloadGrid");
		$selStnLineRelDown.jqGrid('setGridParam', {datatype:"json",postData:{lineId:selLineId,strtusDate:$("#strtusDate").val()},
			loadComplete:function(data){
				$("#stnType option[value='"+(data ? 'LIT00001' : 'LIT00003') + "']").attr("selected", "selected").trigger('change');
				/*if(data){
  					$("#stnType option[value='LIT00001']").attr("selected","selected");
  					$("#hiddenUp").show();
  					$("#hiddenUp2").show();
  				}else{
  					$("#stnType option[value='LIT00003']").attr("selected","selected");
  					$("#hiddenUp").hide();
  					$("#hiddenUp2").hide();
  				}*/
			}
		}).trigger("reloadGrid");
	});
	
    gridInit();
});


	//右移
	function rightMove(right){
		var row= right.getGridParam("reccount");
			var rowIds = $unStnLineRel.grid("getGridParam", "selarrrow");
        	var delIds =right.grid("getGridParam", "selarrrow");
			if (rowIds.length < 1) {
				$.messager.alert("提示","请至少选择一个站点");
				return false;
			}

			var testIds = right.grid("getDataIDs");
			var ss="",newRow="";
			 for(var j=0;j < rowIds.length; j++){
				 ss=rowIds[j]+",";
				 for(var i=0;i < testIds.length; i++){
				   if(rowIds[j]==testIds[i]){
					   ss="";
					   break;
				   }
			     }
			     newRow+=ss;
			 } 
			 newRow=newRow.substring(0,newRow.length-1);
			 if(newRow!=""){
			 	var arrRow=new Array();
				arrRow=newRow.split(",");
			    for (var i = 0; i < arrRow.length; i ++) {
	   			//新增
	   			var rowData = $unStnLineRel.getRowData(arrRow[i]);
	   			right.addRowData(rowData.stnId, {"stnId":rowData.stnId,"stnName":rowData.stnName,"stnCd":rowData.stnCd,"direction":rowData.direction,"stnSn":row+1}, "last");
	   			row++;
			 	}

			}
			 testIds = right.grid("getDataIDs");
			 for (var i = 0; i < testIds.length; i ++) {
				 right.grid('setRowData', testIds[i], {"stnSn":i+1});  
			 }
        for (var i = rowIds.length-1; i>=0; i --) {
            //删除
            $unStnLineRel.grid("delRowData", rowIds[i]);
        }


        $unStnLineRel.resetSelection();
	}
	
	//左移
	function leftMove(left){
		var rowIds =left.grid("getGridParam", "selarrrow");
		if (rowIds.length < 1) {
			$.messager.alert("提示","请至少选择一个站点");
			return false;
		}
		for (var i = rowIds.length-1; i>=0; i --) {
			//删除
			//
            var rowData = left.getRowData(rowIds[i]);
            $unStnLineRel.addRowData(rowData.stnId, {"stnId":rowData.stnId,"stnName":rowData.stnName,"stnCd":rowData.stnCd,"direction":rowData.direction}, "last");
            // reloadGrid();
            // reload();
            left.grid("delRowData", rowIds[i]);
		}
		$unStnLineRel.resetSelection();  
	}
	//上移
	function upMove(up){
		var testIds = up.grid("getDataIDs");
	    var rowIds = up.grid("getGridParam", "selarrrow");
	    var id="", row="";
	    for(var i=0;i < testIds.length; i++){
	   	   if(rowIds==testIds[i]){
	   		   id = testIds[i-1];
	   		   row = i;
	   	   }
	    }
	    up.resetSelection();  
		if(rowIds.length != 1) {
			$.messager.alert("提示","只能移动一个站点");
			return false;
		}else{
			if(id==undefined){
				$.messager.alert("提示","已到达顶端");
			}else{
			//新增
			var rowData = up.getRowData(rowIds);
			up.addRowData(rowData.stnId, {"stnId":rowData.stnId,"stnName":rowData.stnName,"stnCd":rowData.stnCd,"direction":rowData.direction,"stnSn":row}, "before", id);
			up.setRowData(id, {"stnSn":row+1});
			up.grid('setSelection',rowIds);
			//删除
			up.grid("delRowData", rowIds);
			}
		}  
	}
	//下移
	function downMove(down){
		var testIds = down.grid("getDataIDs");
	    var rowIds = down.grid("getGridParam", "selarrrow");
	    var id="", row="";
	    for(var i=0;i < testIds.length; i++){
	   	   if(rowIds==testIds[i]){
	   		   id=testIds[i+1];
	   		   row = i+2;
	   	   }
	    }
	    down.resetSelection();
	    if(rowIds.length > 1) {
	   		$.messager.alert("提示","只能移动一个站点");
	   		return false;
	    }else{
	   		if(id==undefined){
	   			$.messager.alert("提示","已到达底端");
	   		}else{
		   		var rowData = down.getRowData(rowIds);
		   		var stnId=rowData.stnId;
		   		var stnName=rowData.stnName;
		   		var stnCd=rowData.stnCd;
		   		var direction= rowData.direction;
		   		//删除
		   		down.grid("delRowData", rowIds);
		   		//新增
		   		down.addRowData(stnId, {"stnId":stnId,"stnName":stnName,"stnCd":stnCd,"direction":direction,"stnSn":row}, "after", id);
		   		down.setRowData(id, {"stnSn":row-1});
		   		down.grid('setSelection',rowIds);
	   		}
	    }  
	}